home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / isapi / ServletServer.java < prev   
Encoding:
Java Source  |  1997-07-18  |  3.6 KB  |  160 lines

  1. /*
  2.  * @(#)ServletServer.java    1.7 97/07/17
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.isapi;
  23.  
  24. import java.io.*;
  25. import java.util.*;
  26. import javax.servlet.Servlet;
  27. import sun.servlet.*;
  28. import sun.servlet.http.*;
  29. import sun.servlet.util.*;
  30.  
  31. /**
  32.  * This class implements servlet runner for Microsoft Information Server
  33.  *
  34.  * @version    1.7, 07/17/97
  35.  * @author    Jongyoon Lee
  36.  */
  37. public class ServletServer extends HttpServer {
  38.  
  39.     /**
  40.      * The server instance.
  41.      */
  42.     protected static ServletServer server;
  43.  
  44.     /*
  45.      * The loaded classes.
  46.      */
  47.     private Hashtable classes = new Hashtable();
  48.  
  49.     /*
  50.      * The loaded servlets.
  51.      */
  52.     private Hashtable servlets = new Hashtable();
  53.  
  54.     /**
  55.      * Runs the HTTP server.
  56.      */
  57.     public static void main(String args[]) {
  58.     Properties prop = new Properties();
  59.     server = new ServletServer();
  60.     for (int i = 0; i < args.length; i++) {
  61.         int index = args[i].indexOf('=');
  62.         if (index != -1) {
  63.         prop.put(args[i].substring(0, index),
  64.              args[i].substring(index + 1));
  65.         }
  66.     }
  67.     server.loadProperties(prop);
  68.     server.run();
  69.  
  70.     /*
  71.     } catch (Exception e) {
  72.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  73.         PrintStream ps = new PrintStream(baos);
  74.         e.printStackTrace(ps);
  75.         byte[] buf = baos.toByteArray();
  76.         return new String(buf);
  77.     }
  78.     return "FOO";
  79.     */
  80.     }
  81.  
  82.     /**
  83.      * Runs the servlet runner
  84.      */
  85.     public void run() {
  86.     try {
  87.         System.loadLibrary("servlet");
  88.     } catch (Exception e) {
  89.         System.exit(0);
  90.     }
  91.  
  92.     handlers = new ThreadGroup("ServletServer-");
  93.  
  94.     // initialize connection handlers
  95.     connections = new Queue(maxHandlers);
  96.     total = 0;
  97.     avail = 0;
  98.     }
  99.  
  100.     /**
  101.      * Accepts a new connection
  102.      */
  103.     public static void handleConnection(long ecb) {
  104.     server.newConnection(ecb);
  105.     }
  106.  
  107.     private void newConnection(long ecb) {
  108.     //try {
  109.  
  110.     ISAPIConnectionHandler handler = new ISAPIConnectionHandler(this);
  111.     handler.init(ecb);
  112.     handler.run();
  113.  
  114.     /*
  115.     } catch (Exception e) {
  116.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  117.         PrintStream ps = new PrintStream(baos);
  118.         e.printStackTrace(ps);
  119.         byte[] buf = baos.toByteArray();
  120.         return new String(buf);
  121.     }
  122.     return "GOOD!";
  123.     */
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Gets a servlet by name.
  129.      */
  130.     public Servlet getServlet(String name) {
  131.     String code = servletProps.getProperty("servlet." +
  132.                            name + 
  133.                            ".code");
  134.     String args = servletProps.getProperty("servlet." +
  135.                            name + 
  136.                            ".initArgs");
  137.     if (code == null) {
  138.         code = name;
  139.     }
  140.  
  141.     Servlet s = (Servlet) servlets.get(code);
  142.     if (s != null) {
  143.         return s;
  144.     }
  145.     try {
  146.         Class cl = Class.forName(code);
  147.         //Class cl = Class.forName("SnoopServlet");
  148.         if (cl != null) {
  149.         s = (Servlet) cl.newInstance();
  150.         s.init(new HttpServletConfig(this, args));
  151.         servlets.put(name, s);
  152.         }
  153.     } catch (Throwable e) {
  154.         return null;
  155.     }
  156.  
  157.     return s;
  158.     }
  159. }
  160.